package com.app;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.app.adapter.ClassmateListAdapter;
import com.app.bean.ClassmateListBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ClassmateListActivity extends AppCompatActivity {
    private ListView classMateListView;
    private List<ClassmateListBean.ListBean> listBeen;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_class_mate_list);
        initView();
        initData();
        initEvent();
    }

    private void initData() {
        JSONObject jsonObject = new JSONObject();
        try{
            jsonObject.put("className","18移应1班");
        }catch (JSONException e){
            e.printStackTrace();
        }

        RequestQueue requestQueue = Volley.newRequestQueue(ClassmateListActivity.this);
        String url = "http://www.xhjjyst.com/freshmenapp/classmate/listbyclassname";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                Gson gson = new Gson();
                ClassmateListBean classmateListBean = gson.fromJson(jsonObject.toString(), ClassmateListBean.class);
                listBeen = classmateListBean.getList();
                ClassmateListAdapter classmateListAdapter = new ClassmateListAdapter(ClassmateListActivity.this, (ArrayList<ClassmateListBean.ListBean>) listBeen);
                classMateListView.setAdapter(classmateListAdapter);
            }
        },new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(ClassmateListActivity.this,"网络异常"+volleyError,Toast.LENGTH_SHORT).show();
                Log.e("AAA", "onErrorResponse: "+volleyError);
    }
});
        requestQueue.add(jsonObjectRequest);
    }

    private void initView() {
        classMateListView = (ListView) findViewById(R.id.class_mate_listview);
        classMateListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });
    }

    private void initEvent(){
        classMateListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(ClassmateListActivity.this,ClassmateDetailActivity.class);
                intent.putExtra("id",listBeen.get(i).getId()+"");
                startActivity(intent);
            }
        });
    }
}